package com.ternnetwork.objectstore.config;


import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import org.springframework.boot.context.properties.ConfigurationProperties;
import lombok.Data;

import io.minio.MinioClient;
import io.minio.*;

@Configuration
@ConfigurationProperties(prefix = "minio")
@Data
@Slf4j
public class MinioConfiguration {

    private String accessKey;

    private String secretKey;

    private String url;

    private String bucketName;


    @Bean
    public MinioClient minioClient() {
        MinioClient minioClient= MinioClient.builder()
                .endpoint(url)
                .credentials(accessKey, secretKey)
                .build();

       try {
           if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())) {
               minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
           }
       }catch (Exception e){
           log.error("create bucket error:",e);
       }
        return minioClient;
    }
}